//
//  HosDepartmentViewController.m
//  Doctor1.0
//
//  Created by mk on 15/11/19.
//  Copyright (c) 2015年 cocoa-mk. All rights reserved.
//

#import "HosDepartmentViewController.h"
#import "HosListViewController.h"

@interface HosDepartmentViewController ()
{
    NSMutableArray * arrMarks;
    NSMutableDictionary * dicDep;

    NSString * strNextUrl;
}

@end

@implementation HosDepartmentViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    arrMarks = [[NSMutableArray alloc]initWithCapacity:0];
    dicDep = [[NSMutableDictionary alloc]initWithCapacity:0];
    
    titleLabel.text = self.hosInfo.hos_name;
    [self getData];
}


-(void)getData
{
   SetHUDShowText(@"加载中..")
    
    NSString * strUrl = [NSString stringWithFormat:@"/hospital/%@",self.hosInfo.hos_ID];
    
    [[HttpClient manager]GET:strUrl
                  parameters:nil
    success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSMutableDictionary * dic = [responseObject objectForKey:requestDataKey];
        strNextUrl = [dic objectForKey:@"deptUrl"];
        dicDep = [dic objectForKey:@"departments"];
        arrMarks = [NSMutableArray arrayWithArray:[dicDep allKeys]];
        [tableMain reloadData];
        SetHUDHidden;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        SetHUDHidden;
        HUDShowRequestError(error.domain, self.view)
    }];

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return arrMarks.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellTableIdentifier = @"HosDepartmentCellStr";
    HosDepartmentCell *cellLayout = (HosDepartmentCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
    if (!cellLayout)
    {
        cellLayout = (HosDepartmentCell *)[[[NSBundle mainBundle] loadNibNamed:@"HosDepartmentCell" owner:self options:nil] objectAtIndex:0];
    }
    NSMutableArray * array = [NSMutableArray arrayWithArray:[dicDep objectForKey:[arrMarks objectAtIndex:indexPath.row]]];
    [cellLayout fillMrkaDataWith:array title:[arrMarks objectAtIndex:indexPath.row]];
    
    //更新UIView的layout过程和Autolayout
    [cellLayout layoutIfNeeded];
    [cellLayout updateConstraintsIfNeeded];
    //通过systemLayoutSizeFittingSize返回最低高度
    CGFloat height = [cellLayout.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    return height;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellTableIdentifier = @"HosDepartmentCellStr";
        
     HosDepartmentCell *cell = (HosDepartmentCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
     if (!cell)
     {
        cell = (HosDepartmentCell *)[[[NSBundle mainBundle] loadNibNamed:@"HosDepartmentCell" owner:self options:nil] objectAtIndex:0];
        cell.delegate = self;
     }
    
    NSMutableArray * array = [NSMutableArray arrayWithArray:[dicDep objectForKey:[arrMarks objectAtIndex:indexPath.row]]];
    
    [cell fillMrkaDataWith:array title:[arrMarks objectAtIndex:indexPath.row]];
    
    return cell;
}


#pragma mark cellDelegate
-(void)clickMarkeWith:(HosDepartmentCell *)cell selectIndex:(NSInteger)index
{
  
    NSIndexPath *titIndex = [tableMain indexPathForCell:cell];
        
    NSMutableArray * array = [NSMutableArray arrayWithArray:[dicDep objectForKey:[arrMarks objectAtIndex:titIndex.row]]];
        
    HosListViewController * list = [[HosListViewController alloc]initWithNibName:@"HosListViewController" bundle:nil];
    list.strHosName = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index] objectForKey:@"name"]];
    list.strHosID = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index] objectForKey:@"id"]];
    list.strUrl = strNextUrl;
    [self.navigationController pushViewController:list animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end






